<?php
    session_start();
    if($_SESSION['is_login'] !== 'yes'){
        header("Location:/login.php");
        exit;
    }
    define('YANGJIFANG',true);
    require_once "db.php";


    $message = '';

    if($_SERVER['REQUEST_METHOD'] == 'POST'){
        $data = [];
        $data['title'] = isset($_POST['title'])?trim($_POST['title']):'';
        $data['content'] = isset($_POST['content'])?trim($_POST['content']):'';
        $data['add_time'] = time();

        $message = '操作失败';
        if($data['title'] && $data['content'] ){
            $db = new Db();
            $insertId = $db->insert($data,'posts');
            if($insertId) $message = '操作成功';
        }
    }
?>
<html>
    <head>
        <style type="text/css">
            .title{
                padding:10px 0;
            }
            input{
                width:500px;
            }
            .content textarea{
                height:500px;
                width:500px;
            }
            .submit{
                padding:10px 0 10px 50px;
            }
        </style>
    </head>
    <body>
        <div><?php echo $message; ?></div>
        <div>
            <a href="/admin-list.php">列表</a>
            <a href="/logout.php">退出登录</a>
        </div>
        <form action="" method="post">
            <div class="title">
                标题：<input type="text" name="title" />
            </div>
            <div class="content">
                内容：<textarea name="content"></textarea>
            </div>
            <div class="submit">
               <button type="submit">提交</button>
            </div>
        </form>
    </body>
</html>